using System;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.WowAddonStudio.Lua.RuntimeService.Tests.Common;
using Microsoft.WowAddonStudio.Lua.RuntimeService.Tests.Mocks;
using Microsoft.WowAddonStudio.Lua.RuntimeService.Tests.WowBench;
using Microsoft.WowAddonStudio.Lua.RuntimeService.ToolWindows;
using Microsoft.WowAddonStudio.Lua.RuntimeService.WowBench;
using Microsoft.WowAddonStudio.Lua.RuntimeService.WowBench.Resolvers;
using NUnit.Framework;

namespace Microsoft.WowAddonStudio.Lua.RuntimeService.Tests.Controls
{
	[TestFixture]
	public class HistoryToolStripButtonTest : WoWBenchTestBase
	{
		private WoWBenchController handler;
		private ILuaRuntime runtime;

		#region SetUp

		[TestFixtureSetUp]
		public override void TestFixtureSetUp()
		{
			base.TestFixtureSetUp();
			ConfigurationHelper.InitializeServiceLocator(Provider);
			runtime = new LuaRuntime(Provider, new ResolverFactory(Provider), new MockShellHostFactory());
			handler = new WoWBenchController(runtime);
		}

		[SetUp]
		public override void SetUp()
		{
			base.SetUp();
		}

		[TestFixtureTearDown]
		public override void TestFixtureTearDown()
		{
			if(handler != null) handler.Dispose();
			if (runtime != null) runtime.Dispose();
			base.TestFixtureTearDown();

		}

		#endregion

		#region TearDown

		[TearDown]
		public override void TearDown()
		{
			base.TearDown();
		}

		#endregion

		#region Tests

		[Test(Description = "")]
		public void Test_EventSubscription()
		{
			var history = new CommandHistory(handler);
			var button = new HistoryToolStripButton();

			invocationCount = 0;
			button.Initialize(history);
			button.HistoryItemSelected += OnHistoryItemSelectedTest;
			history.Add(new WoWBenchCommand(handler, "1. TestCommand"));
			ToolStripDropDownItem dropDownItem = new ToolStripMenuItem("1. TestCommand");

			//Fire private event
			MethodInfo method = button.GetType().GetMethod("OnCommandHistoryToolStripButtonDropDownItemClicked", BindingFlags.Instance | BindingFlags.NonPublic);
			method.Invoke(button, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null,
			                              new object[] {button, new ToolStripItemClickedEventArgs(dropDownItem)}, null);

			Assert.AreEqual(1, invocationCount);
			button.UnsubscribeEvents();
			method.Invoke(button, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null,
										  new object[] { button, new ToolStripItemClickedEventArgs(dropDownItem) }, null);
			Assert.AreEqual(1, invocationCount);
		}

		private int invocationCount;

		/// <summary>
		/// Called when [history item selected test].
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="Microsoft.WowAddonStudio.Lua.Runtime.ToolWindows.HistoryItemSelectedEventArgs"/> instance containing the event data.</param>
		private void OnHistoryItemSelectedTest(object sender, HistoryItemSelectedEventArgs e)
		{
			Assert.IsNotNull(e);
			Assert.IsNotNull(e.Command);
			Console.WriteLine("OnHistoryItemSelected event: {0}", e.Command);
			invocationCount++;
		}


		[Test(Description = "")]
		[ExpectedException(typeof(ArgumentNullException))]
		public void Test_InitializeFailed()
		{
			var button = new HistoryToolStripButton();
			button.Initialize(null);
		}
		#endregion
	}
}